var role = function(){
    var html = '<div style="margin-left: 10px;margin-right: 10px"><table class="layui-hide" id="test" lay-filter="test"></table></div>';
    layui.use(['table', 'form', 'jquery', 'element', 'laydate'], function () {
        var table = layui.table
            , form = layui.form
            , $ = layui.jquery
            , laydate = layui.laydate
            , element = layui.element;
        $("#mainbody").html(html);
        //表格加载
        table.render({
            elem: '#test'
            , url: '/sys/role/list'
            , toolbar: 'default'
            , title: '角色表'
            , id: 'roleTable' //用作主键 做刷新用
            , page: true
            , limit: 10//默认每页显示数量
            , limits: [10, 20, 30]//每页显示数量类型
            ,cols: [[
                {type: 'checkbox'}
                ,{type: 'numbers',title: '序号',width: 80}
                // ,{field:'roleId', width:80, title: 'ID', sort: true}
                ,{field:'roleName', width:200, title: '角色名'}
                ,{field:'roleKey', width:200, title: '角色code'}
                ,{field:'rolePermission', width:600, title: '拥有权限'}
                ,{field:'roleDecription', width:432, title: '具体描述'}
            ]]
        });

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'add':
                    layer.open({
                        title: ['添加角色', 'font-size:18px;']
                        , shade: 0.6 //遮罩透明度
                        , area: ['630px','500px']//宽度
                        , type: 2
                        , anim: 1 //0-6的动画形式，-1不开启
                        , content: "/sys/role/addButton"
                        , success: function (layero, index) {
                            //layer高度自适应
                            layer.iframeAuto(index);
                            table.reload('roleTable');
                        }
                    })
                    break;
                case 'update':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else if (data.length > 1) {
                        layer.msg('只能同时编辑一个');
                    } else {
                        layer.open({
                            title: ['编辑角色', 'font-size:18px;']
                            , shade: 0.6 //遮罩透明度
                            , area: ['630px','500px']//宽度
                            , type: 2
                            , anim: 1 //0-6的动画形式，-1不开启
                            , content: "/sys/role/editButton/"+data[0].roleId
                            , success: function (layero, index) {
                                //layer高度自适应
                                // layer.iframeAuto(index);
                                table.reload('roleTable');
                            }
                        })
                        break;
                    }
                    break;
                case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        //页面删除效果
                        var arr = new Array();
                        for (var i = 0; i < data.length; i++) {
                            arr.push(data[i].roleId);
                        }
                        layer.confirm("确定删除吗?",{

                                btn: ['确定', '取消']
                                ,title:"提示"
                            }, function (index, layero) {
                                console.log(arr);
                                $.ajax({
                                    type: "POST",
                                    url: "/sys/role/delete",
                                    traditional: true,
                                    dataType: "json",
                                    contentType: "application/json",
                                    data: JSON.stringify(arr),
                                    success: function (data) {
                                        console.log(data);
                                        layer.msg("删除成功!",{ icon: 1, time: 1000 });
                                        table.reload('roleTable');
                                    },
                                    error: function () {

                                        layer.msg(JSON.parse(data.responseText).message,{ icon: 1, time: 1000 });
                                    }
                                });
                            }
                        );
                    }
                    break;
            }
            ;
        });



    });
}